package cc.rengu.igas.mcps.core.service.trans.cardpay;

import cc.rengu.igas.channel.cups.common.constant.CupsConfigConstant;
import cc.rengu.igas.channel.cups.facade.bean.CupsTermInfo;
import cc.rengu.igas.channel.cups.facade.bean.OrigTxnInfo;
import cc.rengu.igas.channel.cups.facade.request.PreAuthCancelRevsalRequest;
import cc.rengu.igas.channel.cups.facade.response.PreAuthCancelRevsalResponse;
import cc.rengu.igas.mcps.common.constant.McpsParamConstant;
import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.enums.McpsAcctTypeEnum;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.model.PosTransInfo;
import cc.rengu.igas.mcps.core.realize.PosParamDealService;
import cc.rengu.igas.mcps.core.realize.impl.PosParamDealServiceImpl;
import cc.rengu.igas.mcps.core.service.base.RevsalService;
import cc.rengu.igas.mcps.facade.request.PosCardPreAuthCancelRevsalRequest;
import cc.rengu.igas.mcps.facade.response.PosCardPreAuthCancelRevsalResponse;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.common.util.GlobalEsbUtil;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.model.MchntTermInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.FinanceTxnMapper;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.FinanceTxnMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.*;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class PosCardPreAuthCancelRevsalService extends RevsalService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest = new PosCardPreAuthCancelRevsalRequest();
        ConvertUtil.convertOutput(posCardPreAuthCancelRevsalRequest);
        return posCardPreAuthCancelRevsalRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse bizResponse = new BizResponse();
        PosCardPreAuthCancelRevsalResponse posCardPreAuthCancelRevsalResponse = new PosCardPreAuthCancelRevsalResponse();
        ConvertUtil.convertOutput(posCardPreAuthCancelRevsalResponse);
        PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest = (PosCardPreAuthCancelRevsalRequest) request;
        if (null == txnInfo) {
            rglog.error("从缓存中获取交易配置信息失败");
            throw new BizException(RespCodeEnum.POS_NOT_SUPPORT.getRespCode(), RespCodeEnum.POS_NOT_SUPPORT.getRespDesc());
        }
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sysNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        //获取原交易商户订单
        MchntOrderInfo origMchntOrderInfo = (MchntOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_MCHNT_ORDER_INFO);
        //获取原交易用户订单
        UserOrderInfo origUserOrderInfo = (UserOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_USER_ORDER_INFO);

        PosParamDealService posParamDealService = new PosParamDealServiceImpl();
        //获取原通道支付订单
        PayOrderInfo origPayOrderInfoESB = (PayOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_PAY_ORDER_INFO);

        //获取原流水
        FinanceTxnMapper financeTxnMapper = new FinanceTxnMapperImpl();
        FinanceTxn oriFinanceTxn = financeTxnMapper.selectFinanceTxnInfoByPrimaryKey(origPayOrderInfoESB.getInstId(), origPayOrderInfoESB.getSysSeqNum());
        if (null == oriFinanceTxn) {
            rglog.error("此时为数据库问题导致");
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR);
        }

        //调用esb银数预授权撤销冲正接口
        if (origPayOrderInfoESB.getPayerAcctType().equals(McpsAcctTypeEnum.OWN_CREDIT.getAcctType())) {
            updcPreAuthCancelRevsal(oriFinanceTxn, origPayOrderInfoESB, origMchntOrderInfo, origUserOrderInfo, posCardPreAuthCancelRevsalRequest, bizResponse, posParamDealService);
        }
        //调用银联预授权撤销冲正接口
        if (origPayOrderInfoESB.getPayerAcctType().equals(McpsAcctTypeEnum.OTHER_DEBIT.getAcctType()) ||
                origPayOrderInfoESB.getPayerAcctType().equals(McpsAcctTypeEnum.OTHER_CREDIT.getAcctType())) {
            cupsPreAuthCancelRevsal(oriFinanceTxn, origPayOrderInfoESB, origMchntOrderInfo, origUserOrderInfo, posCardPreAuthCancelRevsalRequest, bizResponse, posParamDealService, posCardPreAuthCancelRevsalResponse);
        }

        /* 重组响应结果 */
        cc.rengu.igas.mcps.facade.base.Header header = new cc.rengu.igas.mcps.facade.base.Header();
        BeanUtil.beanCopy(posCardPreAuthCancelRevsalRequest.getHeader(), header);
        posCardPreAuthCancelRevsalResponse.setHeader(header);
        ////posCardPreAuthCancelRevsalResponse.setEpayRrn(sysNo);
//        posCardPreAuthCancelRevsalResponse.setReferenceNo();
        bizResponse.setRspSysId(AppParamConstant.IGAS);
        bizResponse.setResult(posCardPreAuthCancelRevsalResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PosCardPreAuthCancelRevsalResponse posCardPreAuthCancelRevsalResponse = (PosCardPreAuthCancelRevsalResponse) bizResponse.getResult();
        posCardPreAuthCancelRevsalResponse.setRespCode(bizResponse.getRespCode());
        posCardPreAuthCancelRevsalResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(posCardPreAuthCancelRevsalResponse);
    }

    /**
     * 登记用户订单表
     *
     * @param posCardPreAuthCancelRevsalRequest 请求信息
     * @throws Exception 异常
     */
    private UserOrderInfo initOrderInfoUser(PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        UserOrderInfo userOrderInfo = new UserOrderInfo();
        /*获取原用户订单表*/
        UserOrderInfo oriUserOrderInfo = (UserOrderInfo) GlobalTransUtil.getCacheValue(TransCacheConstant.ORIG_USER_ORDER_INFO);
        /*获取机构号*/
        String instId = posCardPreAuthCancelRevsalRequest.getHeader().getInstId();
        /*获取交易码*/
        String txnNum = posCardPreAuthCancelRevsalRequest.getHeader().getTxnNum();
        /*获取业务细分*/
        String bizType = posCardPreAuthCancelRevsalRequest.getHeader().getBizType();
        /*获取源系统标识*/
        String msgSrcId = posCardPreAuthCancelRevsalRequest.getHeader().getSrcSysId();
        userOrderInfo.setInstId(instId);
        userOrderInfo.setTxnDate(DateUtil.getCurrentDate());
        userOrderInfo.setTxnTime(DateUtil.getCurrentTime());
        userOrderInfo.setTxnNum(txnNum);
        userOrderInfo.setBizType(bizType);
        userOrderInfo.setMsgSrcId(msgSrcId);
        userOrderInfo.setTransChannelId(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TRANS_CHANNEL_ID));
        userOrderInfo.setMchntOrderId(posCardPreAuthCancelRevsalRequest.getHeader().getTraceNo());
        userOrderInfo.setSysSeqNum(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM));
        userOrderInfo.setOrderCreateTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
        //userOrderInfo.setOrderExpiryTime(xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.ORDER_EXPIRY_TIME));
        userOrderInfo.setOrderExpiryTime(DateUtil.getDateByTimeStamp(System.currentTimeMillis() + 5 * 60 * 1000, "yyyyMMddHHmmss"));
        userOrderInfo.setOrderStatus(OrderStatusEnum.INIT.getStatus());
        userOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        userOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        userOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        userOrderInfo.setOrderAmt(oriUserOrderInfo.getOrderAmt());
        userOrderInfo.setOrigSysSeqNum(oriUserOrderInfo.getSysSeqNum());
        userOrderInfo.setOrigTxnDate(oriUserOrderInfo.getTxnDate());
        userOrderInfo.setFrontSeqNum(posCardPreAuthCancelRevsalRequest.getHeader().getTraceNo());
        return userOrderInfo;
    }

    /**
     * 登记商户订单表
     *
     * @param posCardPreAuthCancelRevsalRequest 请求信息
     * @throws Exception 异常
     */
    private MchntOrderInfo initOrderInfoMchnt(PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest, UserOrderInfo userOrderInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MchntOrderInfo mchntOrderInfo = new MchntOrderInfo();
        /*获取商户基本信息*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(posCardPreAuthCancelRevsalRequest.getHeader().getInstId(), posCardPreAuthCancelRevsalRequest.getMchntNo());
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", posCardPreAuthCancelRevsalRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*内部树复制会把订单状态保存到商户订单信息表*/
        BeanUtil.beanCopy(userOrderInfo, mchntOrderInfo);
        /*设置商户订单表中业务产品代码*/
        mchntOrderInfo.setBizProdCode(txnInfo.getSrcTxnAuthCfg().getBizProdCode());
        /*收单机构号   从系统参数表中获取value值 即为收单机构号*/
        mchntOrderInfo.setAcqInstCode(mchntInfo.getSignInstId());
        /*行业类别*/
        mchntOrderInfo.setIndustryType(mchntInfo.getIndustryType());
        /*商户号*/
        mchntOrderInfo.setMchntNo(posCardPreAuthCancelRevsalRequest.getMchntNo());
        /*商户名称*/
        mchntOrderInfo.setMchntName(mchntInfo.getMchntName());
        /*交易币种*/
        mchntOrderInfo.setOrderCcy(AppParamConstant.DEFAULT_ORDER_CCY);
        /*交易金额*/
        mchntOrderInfo.setRealSettleAmt(userOrderInfo.getOrderAmt());
        /*实时清算标识*/
        mchntOrderInfo.setRealSettleFlag(mchntInfo.getRealSettleFlag());
        /*设置交易状态*/
        mchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
        /*设置业务状态*/
        mchntOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
        /*创建时间*/
        mchntOrderInfo.setCreateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        /*设置终端号*/
        mchntOrderInfo.setTermNo(posCardPreAuthCancelRevsalRequest.getTermNo());
        return mchntOrderInfo;
    }

    /*
     * 调用银数冲正
     * */
    private void updcPreAuthCancelRevsal(FinanceTxn oriFinanceTxn, PayOrderInfo oriPayOrderInfoESB, MchntOrderInfo origMchntOrderInfo,
                                         UserOrderInfo origUserOrderInfo, PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest,
                                         BizResponse bizResponse, PosParamDealService posParamDealService) throws Exception {

        // TODO 预授权冲正
        // (1) 通讯级失败，返回失败
        // （2） 交易成功，更新原订单表已冲正，返回成功
        // （3） 交易超时，返回超时
    }

    /*
     * 调用银联冲正
     * */
    private void cupsPreAuthCancelRevsal(FinanceTxn oriFinanceTxn, PayOrderInfo oriPayOrderInfoESB, MchntOrderInfo origMchntOrderInfo,
                                         UserOrderInfo origUserOrderInfo, PosCardPreAuthCancelRevsalRequest posCardPreAuthCancelRevsalRequest,
                                         BizResponse bizResponse, PosParamDealService posParamDealService,
                                         PosCardPreAuthCancelRevsalResponse posCardPreAuthCancelRevsalResponse) throws Exception {
        DubboService dubboService = new DubboServiceImpl();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.INST_ID);
        String sysNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*初始化订单服务*/
        OrderService orderService = new OrderServiceImpl();
        /*获取商户基本信息*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(instId, origMchntOrderInfo.getMchntNo());
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", origMchntOrderInfo.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*准备请求报文*/
        PreAuthCancelRevsalRequest preAuthCancelRevsalRequest = new PreAuthCancelRevsalRequest();
        /*设置请求报文*/
        cc.rengu.igas.channel.cups.facade.base.Header header = new cc.rengu.igas.channel.cups.facade.base.Header();
        BeanUtil.beanCopy(posCardPreAuthCancelRevsalRequest.getHeader(), header);
        ChannelCallCfg channelCallCfgList = txnInfo.getChannelCallCfgList().stream().filter(item -> ChannelEnum.CUPS.getChannelType().equals(item.getCallChannelId())).collect(Collectors.toList()).get(0);
        /*通道交易码*/
        header.setTxnNum(channelCallCfgList.getCallChannelTxn());
        header.setTraceNo(GlobalEsbUtil.generateEsbSeqeuenceNumber(xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.INST_ID)));
        header.setTransDate(oriFinanceTxn.getTxnDate());
        header.setTransTime(oriFinanceTxn.getTxnTime());
        preAuthCancelRevsalRequest.setHeader(header);
        preAuthCancelRevsalRequest.setCupsTraceNo(sysNum.substring(sysNum.length() - 6));
        String txnDate = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_DATE);
        String txnTime = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.TXN_TIME);
        preAuthCancelRevsalRequest.setTransDateTime(txnDate.substring(4, 8) + txnTime);
        preAuthCancelRevsalRequest.setCupsMchntNo(mchntInfo.getCupsMchntNo());
        preAuthCancelRevsalRequest.setTermNo(posCardPreAuthCancelRevsalRequest.getTermNo());
        preAuthCancelRevsalRequest.setMchntShortName(mchntInfo.getMchntSimpleName());
        preAuthCancelRevsalRequest.setMchntType(mchntInfo.getIndustryType());
        String acqInstCode = posParamDealService.genCupsAcqInstCode(instId, posCardPreAuthCancelRevsalRequest.getMchntNo());
        String fwdInstCode = XmlConfigUtil.getXmlConfigCacheKeyValue(CupsConfigConstant.CUPS_CONF_NODE, CupsConfigConstant.CUPS_FWD_INS_CODE);
        preAuthCancelRevsalRequest.setCupsInstCode(acqInstCode);
        preAuthCancelRevsalRequest.setFwdInstCode(fwdInstCode);
        preAuthCancelRevsalRequest.setOrderAmt(oriPayOrderInfoESB.getRealPayAmt());
        preAuthCancelRevsalRequest.setOrderCcy(McpsParamConstant.INNER_DEFAULT_ORDERCCY);
        preAuthCancelRevsalRequest.setPayAcctNo(oriPayOrderInfoESB.getPayerAcctInfo());
        String posTransString = new String(Base64.decodeBase64(oriPayOrderInfoESB.getPayerRemark()));
        PosTransInfo posTransInfo = JSON.parseObject(posTransString, PosTransInfo.class);
        preAuthCancelRevsalRequest.setEntryMode(posTransInfo.getEntryMode());
        preAuthCancelRevsalRequest.setCardSeqNo(posTransInfo.getCardSeqNo());
        preAuthCancelRevsalRequest.setIcConCode(posTransInfo.getIcConCode());
        preAuthCancelRevsalRequest.setTermRead(posTransInfo.getTermRead());
        preAuthCancelRevsalRequest.setReferenceNo(posTransInfo.getRefNo());
        if (!StringUtil.isEmptyOrNull(oriPayOrderInfoESB.getTxnAuthCode())) {
            preAuthCancelRevsalRequest.setAuthCode(oriPayOrderInfoESB.getTxnAuthCode());
        }
        /*设置终端信息*/
        CupsTermInfo cupsTermInfo = new CupsTermInfo();
        BeanUtil.beanCopy(posCardPreAuthCancelRevsalRequest.getTermInfo(), cupsTermInfo);
        if (StringUtil.isEmptyOrNull(cupsTermInfo.getDeviceType())) {
            MchntTermInfo mchntTermInfo = mchntService.getMchntTermInfo(instId, posCardPreAuthCancelRevsalRequest.getMchntNo(), posCardPreAuthCancelRevsalRequest.getTermNo());
            cupsTermInfo.setDeviceType(mchntTermInfo.getTermType());
        } else {
            cupsTermInfo.setDeviceType(posTransInfo.getDeviceType());
        }
        preAuthCancelRevsalRequest.setTermInfo(cupsTermInfo);
        /*设置原交易信息*/
        OrigTxnInfo origTxnInfo = new OrigTxnInfo();
        origTxnInfo.setOriTraceNo(oriPayOrderInfoESB.getChannelOrderId());
        origTxnInfo.setOriTxnDate(oriPayOrderInfoESB.getTxnDate());
        origTxnInfo.setOriTransDateTime(oriPayOrderInfoESB.getTxnDate().substring(4, 8) + oriPayOrderInfoESB.getTxnTime());
        origTxnInfo.setOriCupsTraceNo(oriPayOrderInfoESB.getSysSeqNum().substring(sysNum.length() - 6, sysNum.length()));
        preAuthCancelRevsalRequest.setOrigTxnInfo(origTxnInfo);

        posCardPreAuthCancelRevsalResponse.setReferenceNo(preAuthCancelRevsalRequest.getReferenceNo());
        /*调起服务*/
        cc.rengu.igas.channel.cups.facade.result.Result<PreAuthCancelRevsalResponse> result = (cc.rengu.igas.channel.cups.facade.result.Result<PreAuthCancelRevsalResponse>) dubboService.callDubboService("cupsPayService", "preAuthCancelRevsal", preAuthCancelRevsalRequest);
        rglog.debug("dubbo调用银行卡预授权撤销冲正结果：{}", JSON.toJSONString(result));
        /*失败处理*/
        if (null == result || null == result.getResult() || (!result.getResult().getRespCode().equals(McpsParamConstant.DUBBO_SUCCESS) && !result.getResult().getRespCode().equals(McpsParamConstant.DUBBO_OVERTIME) && !McpsParamConstant.COMMUNICATION_OVERTIME.equals(result.getResult().getRespCode()))) {
            bizResponse.setRespCode(RespCodeEnum.TRANS_FAIL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_FAIL.getRespDesc());
        } else if (result.isSuccess() && McpsParamConstant.DUBBO_SUCCESS.equals(result.getResult().getRespCode())) {
            List<MchntOrderInfo> oriMchntOrderInfoList = new ArrayList<>();
            //更新原支付订单表已冲正
            oriPayOrderInfoESB.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            //更新原商户订单表已冲正
            origMchntOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            oriMchntOrderInfoList.add(origMchntOrderInfo);
            //更新原用户订单表已冲正
            origUserOrderInfo.setBizStatus(BizStatusEnum.REVSAL.getStatus());
            orderService.updateOrderInfo(origUserOrderInfo, oriMchntOrderInfoList, oriPayOrderInfoESB);
            //查询预授权撤销的原交易，并更新原交易的业务状态
            UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
            UserOrderInfo preAuthUserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, origUserOrderInfo.getOrigSysSeqNum());
            if (null != preAuthUserOrderInfo) {
                preAuthUserOrderInfo.setBizStatus(BizStatusEnum.AUTH_INCOMPLETE.getStatus());
                MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
                List<MchntOrderInfo> preAuthMchntOrderInfoList = mchntOrderInfoMapper.selectMchntOrderInfoBySysSeqNum(instId, preAuthUserOrderInfo.getSysSeqNum());
                if (null != preAuthMchntOrderInfoList) {
                    preAuthMchntOrderInfoList.forEach(item -> item.setBizStatus(BizStatusEnum.AUTH_INCOMPLETE.getStatus()));
                }
                PayOrderInfo preAuthPayOrderInfo = null;
                PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
                List<PayOrderInfo> preAuthPayOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, preAuthUserOrderInfo.getSysSeqNum());
                if (null != preAuthPayOrderInfoList) {
                    preAuthPayOrderInfoList = preAuthPayOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.CUPS.getChannelType())).collect(Collectors.toList());
                    preAuthPayOrderInfo = preAuthPayOrderInfoList.get(0);
                    preAuthPayOrderInfo.setSettleDate(result.getResult().getSettleDate());
                    preAuthPayOrderInfo.setBizStatus(BizStatusEnum.AUTH_INCOMPLETE.getStatus());
                }
                orderService.updateOrderInfo(preAuthUserOrderInfo, preAuthMchntOrderInfoList, preAuthPayOrderInfo);
            }
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        } else {
            bizResponse.setRespCode(RespCodeEnum.POS_RSP_TIME_OUT.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.POS_RSP_TIME_OUT.getRespCode());
        }
    }
}